package sa.com.sure.ejtimaa.components;

import java.text.ParseException;

import sa.com.sure.ejtimaa.R;
import sa.com.sure.ejtimaa.common.AppHelper;
import sa.com.sure.ejtimaa.common.Helper;
import sa.com.sure.ejtimaa.database.MeetingAttendees;
import sa.com.sure.ejtimaa.database.MeetingPlaces;
import sa.com.sure.ejtimaa.database.MeetingSessions;
import sa.com.sure.ejtimaa.database.MeetingStatuses;
import sa.com.sure.ejtimaa.model.MeetingInfo;
import sa.com.sure.ejtimaa.model.MeetingPlaceInfo;
import sa.com.sure.ejtimaa.model.MeetingSessionInfo;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MeetingDetailComponent extends LinearLayout {

	public MeetingDetailComponent(Context context,AttributeSet attrs){		
		super(context,attrs);
		
		final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.meeting_detail_component, this);
	}
	
	public void SetData(MeetingInfo item){
        TextView tvTitle = (TextView) getRootView().findViewById(R.id.tvTitle);
        TextView tvMeetingNo = (TextView) getRootView().findViewById(R.id.tvMeetingNo);
        TextView tvMeetingStatus = (TextView) getRootView().findViewById(R.id.tvMeetingStatus);
        TextView tvStartDate = (TextView) getRootView().findViewById(R.id.tvStartDate);
        TextView tvEndDate = (TextView) getRootView().findViewById(R.id.tvEndDate);	 
        TextView tvMeetingPlace = (TextView) getRootView().findViewById(R.id.tvMeetingPlace);	 
        TextView tvSession = (TextView) getRootView().findViewById(R.id.tvSession);	
        TextView tvWaitingForAttendeesReply = (TextView) getRootView().findViewById(R.id.tvWaitingForAttendeesReply);	 
        TextView tvAttendance = (TextView) getRootView().findViewById(R.id.tvAttendance);	
        TextView tvAbsence = (TextView) getRootView().findViewById(R.id.tvAbsence);	
        
    	tvTitle.setText(item.GetTitle());
    	tvMeetingNo.setText(item.GetMeetingNo());
    	tvMeetingStatus.setText(MeetingStatuses.Instance.GetItem(item.GetMeetingStatusID()).GetMeetingStatusName());	     	
    	tvWaitingForAttendeesReply.setText(Integer.toString((item.GetWaitingForAttendeesReply())));
    	tvAbsence.setText(Integer.toString((MeetingAttendees.Instance.GetItemsCountByMeetingIDAndAttended(item.GetMeetingID(),false))));		                        	
    	tvAttendance.setText(Integer.toString(MeetingAttendees.Instance.GetItemsCountByMeetingIDAndAttended(item.GetMeetingID(),true)));

    	MeetingSessionInfo meetingSession =  MeetingSessions.Instance.GetItem(item.GetMeetingID());
    	if(meetingSession != null)
    		tvSession.setText(meetingSession.GetSessionName());
    	else
    		tvSession.setText(AppHelper.getAppContext().getString(R.string.Nothing));
    	
    	MeetingPlaceInfo meetingPlace =  MeetingPlaces.Instance.GetItem(item.GetMeetingID());
    	if(meetingSession != null)
    		tvMeetingPlace.setText(meetingPlace.GetMeetingPlaceName());
    	else
    		tvMeetingPlace.setText(AppHelper.getAppContext().getString(R.string.Nothing));    	
    	
    	try
    	{		                        	
		    tvStartDate.setText(Helper.GetDateAsStringFormat(item.GetStartDate(), Helper.DateFormatLabel));						
        	tvEndDate.setText(Helper.GetDateAsStringFormat(item.GetEndDate(), Helper.DateFormatLabel));
		} catch (ParseException ex) {
			Helper.HandleError(ex);
		}
	}
	
	
}
